package com.GameSpace.CoreAssets;

import java.util.ArrayList;

import com.GameSpace.SameGame.R;

import com.GameSpace.CoreAssets.Option;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class Statistics extends ListActivity {
	
	//private OptionManager om;
	
    private ArrayList<Option> m_orders = null;
    private OptionAdapter m_adapter;
    private Runnable viewOrders;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        
        setContentView(R.layout.settings);
        m_orders = new ArrayList<Option>();
        
        this.m_adapter = new OptionAdapter(this, R.layout.inttypeoption, m_orders);
        setListAdapter(this.m_adapter);
       
        viewOrders = new Runnable(){
            @Override
            public void run() {
                getOrders();
            }
        };
        
        Thread thread =  new Thread(null, viewOrders);
        thread.start();
    }
    
    
    private Runnable returnRes = new Runnable() {

        @Override
        public void run() {
            if(m_orders != null && m_orders.size() > 0){
                m_adapter.notifyDataSetChanged();
                for(int i=0;i<m_orders.size();i++)
                m_adapter.add(m_orders.get(i));
            }
            m_adapter.notifyDataSetChanged();
        }
    };
    
    private void getOrders(){
    	
    	//om=new OptionManager(getBaseContext());
        //Option[] options = new Option[om.getAllOptions().length];
        //options=om.getAllOptions();
    	
        Option option = new Option();
        option.setType("integer");
        option.setLabel("Statistic1");
        option.setValue("34");
        m_orders = new ArrayList<Option>();
        m_orders.add(option);  
        option = new Option();
        option.setType("double");
        option.setLabel("Statistic2");
        option.setValue("12.3");
        m_orders.add(option);   
   
        runOnUiThread(returnRes);
    }

    private class OptionAdapter extends ArrayAdapter<Option> {

        private ArrayList<Option> items;

        public OptionAdapter(Context context, int textViewResourceId, ArrayList<Option> items) {
                super(context, textViewResourceId, items);
                this.items = items;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                
                Option o = items.get(position);
                if (o != null) {
                	LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        	
                	v = vi.inflate(R.layout.inttypeoption, null);
                	TextView tt = (TextView) v.findViewById(R.id.TextView01);
                	TextView bt = (TextView) v.findViewById(R.id.TextView02);
                	if (tt != null) {
                		tt.setText(o.getLabel());                            }
                	if(bt != null){
                		bt.setText(o.getValue());
                	}

                }
                return v;
        }
}
}